package a66weding.com.jiehuntong.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;

import org.xutils.x;

import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.activity.FriendshelptaskActivity;
import a66weding.com.jiehuntong.beans.FriendsListBean;
import a66weding.com.jiehuntong.common.utils.ImageOptionsUtil;
import a66weding.com.jiehuntong.inner.OnItemClickLitener;
import a66weding.com.jiehuntong.widgets.CircularImage;

/**
 * Created by admin on 2016/7/7.
 */
public class FriendListAdapter extends BaseRecyclerAdapter<FriendListAdapter.ViewHolder> {
    //如果有点击事件请设置一个监听;
    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    private Context context;
    private List<FriendsListBean.DataBean> data;
    public FriendListAdapter(Context context, List<FriendsListBean.DataBean> data){
        this.context = context;
        this.data = data;
    }

    @Override
    public ViewHolder getViewHolder(View view) {
        return new ViewHolder(view);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View v = LayoutInflater.from(context).inflate(
                R.layout.item_diyprogress, parent, false);
        ViewHolder holder = new ViewHolder(v);
        return holder;
    }
    //作绑定数据用的;
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position, boolean isItem) {
        holder.name.setText(data.get(position).getName());
        if (mOnItemClickLitener!=null){
            holder.colorGray2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.colorGray2,pos);
                }
            });
        }
        if (data.get(position).getIsAgreeA()==1){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //跳转到任务界面;
                    Intent intent = new Intent(context, FriendshelptaskActivity.class);
                    intent.putExtra("_id", "" + data.get(position).getMemberRID());
                    context.startActivity(intent);
                }
            });
        }
        x.image().bind(holder.head,data.get(position).getNvc_TitlePhoto(),ImageOptionsUtil.getOptions92_92());
        if (data.get(position).getIsAgreeA()==0){
            holder.colorGray2.setVisibility(View.VISIBLE);
            holder.diy_progress_flag.setVisibility(View.INVISIBLE);

        }else {
            holder.colorGray2.setVisibility(View.INVISIBLE);
            holder.diy_progress_flag.setVisibility(View.VISIBLE);
        }
    }

    public void append(List<FriendsListBean.DataBean> data) {
        if (this.data != null) {
            this.data.addAll(data);
        } else {
            this.data = data;
        }
        notifyDataSetChanged();
    }
    @Override
    public int getAdapterItemCount() {
        if(data==null){
            return 0;
        }else{
            return data.size();
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        //头像
        public CircularImage head;
         TextView  name;
        public  TextView colorGray2;
         ImageView diy_progress_flag;
        public ViewHolder(View itemView) {
            super(itemView);
            head = (CircularImage) itemView.findViewById(R.id.diyprogress_roundimage);
            name = (TextView) itemView.findViewById(R.id.item_diyprog_name);
            colorGray2  = (TextView) itemView.findViewById(R.id.istainjia);
            diy_progress_flag = (ImageView) itemView.findViewById(R.id.diy_progress_flag);
        }
    }
    public void clear() {
        if (data != null) {
            this.data.clear();
        }
    }

}
